package com.adds.lvds.service.impl.organize;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.adds.lvds.core.utils.CommUtils;
import com.adds.lvds.dao.impl.organize.SysUserPermissionMapper;
import com.adds.lvds.model.organize.SysUserPermission;

@Service("sysUserPermissionService")
public class SysUserPermissionService {
	@Autowired
	private SysUserPermissionMapper sysUserPermissionMapper;
	
	/**
	 * 批量插入SysUserPermission
	 * 
	 * @param SysUserPermission
	 * @return
	 * @throws Exception
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String addSysUserPermissions(
			SysUserPermission sysUserPermissions) {
		String result = CommUtils.SUCC;
		try {
				sysUserPermissionMapper.insertSysUserPermission(sysUserPermissions);
		} catch (Exception e) {
			result = CommUtils.ERROR;
			throw new RuntimeException();
		}
		return result;
	}
	
	/**
	 * 根据user_id删除权限
	 * @param map
	 * @return
	 */
	@Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
	public String deletePermissionByUserId(Integer user_id){
		String result = CommUtils.SUCC;
		try {
			sysUserPermissionMapper.deletePermissionByUserId(user_id);
		} catch (Exception e) {
			result = CommUtils.ERROR;
			throw new RuntimeException();
		}
		return result;
	}
	
	/**
	 * 根据user_id获取员工所有权限项
	 * **/
	public List<SysUserPermission> findSysUserPermissionByUserId(int user_id){
		List<SysUserPermission> sysUserPermissions = new ArrayList<SysUserPermission>();
		//根据userid获取权限
		try {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("user_id", user_id);
			sysUserPermissions = sysUserPermissionMapper.findSysUserPermissionByMap(map);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return sysUserPermissions;
	}
	
	/**
	 * 根据user_id获取员工所管辖机构id
	 * @param user_id
	 * @return int SysUserPermission
	 * @throws Exception
	 */
	public SysUserPermission findSysUserOrgPermissionByUserId(int user_id){
		SysUserPermission sysUserPermission = new SysUserPermission();
		try {
			//根据userid获取权限
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("user_id", user_id);
			map.put("type", "1");
			sysUserPermission = sysUserPermissionMapper.findSysUserPermissionByMap(map).get(0);
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sysUserPermission;
	}
	
	/**
	 * 根据user_id获取员工所管辖机构id
	 * @param user_id
	 * @return int org_id
	 * @throws Exception
	 */
	public int findSysUserOrgIdByUserId(int user_id){
		int org_id = -1;
		SysUserPermission sysUserPermission = new SysUserPermission();
		try {
			//根据userid获取权限
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("user_id", user_id);
			map.put("type", "1");
			sysUserPermission = sysUserPermissionMapper.findSysUserPermissionByMap(map).get(0);
			//获取所管辖机构id
			org_id = sysUserPermission.getOrg_id();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return org_id;
	}
	
	/**
	 * 根据user_id获取员工所有应用集合
	 * @param user_id
	 * @return List SysUserPermission
	 * @throws Exception
	 */
	public List<SysUserPermission> findSysUserApplicationsByUserId(int user_id,String type){
		List<SysUserPermission> sysUserPermissionList = new ArrayList<SysUserPermission>();
		try {
			//根据userid获取权限
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("user_id", user_id);
			map.put("type", type);
			sysUserPermissionList = sysUserPermissionMapper.findSysUserPermissionByMap(map);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sysUserPermissionList;
	}
}
